@echo off
setlocal enabledelayedexpansion

:: 设置颜色代码
set "GREEN=[32m"
set "RED=[31m"
set "YELLOW=[33m"
set "BLUE=[34m"
set "RESET=[0m"

:: 设置时间戳
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "timestamp=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%"

echo %BLUE%========================================%RESET%
echo %BLUE%HarmonyOS 自动化测试执行脚本%RESET%
echo %BLUE%执行时间: %timestamp%%RESET%
echo %BLUE%========================================%RESET%
echo.

cd /d "%~dp0.."

:: 创建测试报告目录
if not exist "test-reports" mkdir "test-reports"
set "REPORT_DIR=test-reports\%timestamp%"
mkdir "%REPORT_DIR%"

echo %YELLOW%[1/6] 环境验证...%RESET%
call scripts\verify-harmonyos-env.bat > "%REPORT_DIR%\env-check.log" 2>&1
if %errorlevel%==0 (
    echo %GREEN%✓ 环境验证通过%RESET%
) else (
    echo %RED%✗ 环境验证失败，请检查 %REPORT_DIR%\env-check.log%RESET%
    goto :error_exit
)
echo.

echo %YELLOW%[2/6] 清理构建缓存...%RESET%
if exist "ohos\.hvigor" (
    rmdir /s /q "ohos\.hvigor"
    echo %GREEN%✓ 清理完成%RESET%
) else (
    echo %GREEN%✓ 无需清理%RESET%
)

if exist "ohos\entry\build" (
    rmdir /s /q "ohos\entry\build"
    echo %GREEN%✓ 构建目录清理完成%RESET%
)
echo.

echo %YELLOW%[3/6] 依赖检查和安装...%RESET%
cd ohos
call ohpm install > "..\%REPORT_DIR%\ohpm-install.log" 2>&1
if %errorlevel%==0 (
    echo %GREEN%✓ 依赖安装成功%RESET%
) else (
    echo %YELLOW%! 依赖安装有警告，继续执行...%RESET%
)
echo.

echo %YELLOW%[4/6] 构建测试应用...%RESET%
echo 正在构建 HarmonyOS 应用...
call hvigor assembleHap --mode module -p product=default > "..\%REPORT_DIR%\build.log" 2>&1
if %errorlevel%==0 (
    echo %GREEN%✓ 构建成功%RESET%
) else (
    echo %RED%✗ 构建失败，详细信息请查看 %REPORT_DIR%\build.log%RESET%
    goto :error_exit
)
echo.

echo %YELLOW%[5/6] 运行测试套件...%RESET%
echo 正在执行 HarmonyOS 验证测试...

:: 运行不同类型的测试
echo   - 运行单元测试...
call hvigor test --mode module -p product=default > "..\%REPORT_DIR%\unit-tests.log" 2>&1
set "unit_result=%errorlevel%"

echo   - 运行集成测试...
call hvigor test --mode module -p product=default --tests HarmonyOSValidation > "..\%REPORT_DIR%\integration-tests.log" 2>&1
set "integration_result=%errorlevel%"

echo   - 运行性能测试...
call hvigor test --mode module -p product=default --tests Performance > "..\%REPORT_DIR%\performance-tests.log" 2>&1
set "performance_result=%errorlevel%"

cd ..
echo.

echo %YELLOW%[6/6] 生成测试报告...%RESET%

:: 创建HTML测试报告
echo ^<!DOCTYPE html^> > "%REPORT_DIR%\test-report.html"
echo ^<html^>^<head^>^<title^>HarmonyOS 测试报告^</title^>^</head^> >> "%REPORT_DIR%\test-report.html"
echo ^<body^>^<h1^>HarmonyOS 自动化测试报告^</h1^> >> "%REPORT_DIR%\test-report.html"
echo ^<p^>执行时间: %timestamp%^</p^> >> "%REPORT_DIR%\test-report.html"

if %unit_result%==0 (
    echo ^<p style="color:green"^>✓ 单元测试: 通过^</p^> >> "%REPORT_DIR%\test-report.html"
    echo %GREEN%✓ 单元测试通过%RESET%
) else (
    echo ^<p style="color:red"^>✗ 单元测试: 失败^</p^> >> "%REPORT_DIR%\test-report.html"
    echo %RED%✗ 单元测试失败%RESET%
)

if %integration_result%==0 (
    echo ^<p style="color:green"^>✓ 集成测试: 通过^</p^> >> "%REPORT_DIR%\test-report.html"
    echo %GREEN%✓ 集成测试通过%RESET%
) else (
    echo ^<p style="color:red"^>✗ 集成测试: 失败^</p^> >> "%REPORT_DIR%\test-report.html"
    echo %RED%✗ 集成测试失败%RESET%
)

if %performance_result%==0 (
    echo ^<p style="color:green"^>✓ 性能测试: 通过^</p^> >> "%REPORT_DIR%\test-report.html"
    echo %GREEN%✓ 性能测试通过%RESET%
) else (
    echo ^<p style="color:orange"^>! 性能测试: 有警告^</p^> >> "%REPORT_DIR%\test-report.html"
    echo %YELLOW%! 性能测试有警告%RESET%
)

echo ^</body^>^</html^> >> "%REPORT_DIR%\test-report.html"

:: 复制测试结果文件
if exist "ohos\entry\build\test\reports" (
    xcopy "ohos\entry\build\test\reports\*" "%REPORT_DIR%\detailed-reports\" /E /I /Q
    echo %GREEN%✓ 详细测试报告已复制%RESET%
)

echo.
echo %BLUE%========================================%RESET%
echo %BLUE%测试执行完成%RESET%
echo %BLUE%========================================%RESET%
echo.
echo %GREEN%测试报告位置：%RESET%
echo   - 主报告: %REPORT_DIR%\test-report.html
echo   - 详细日志: %REPORT_DIR%\
echo   - HarmonyOS原生报告: ohos\entry\build\test\reports\
echo.
echo %BLUE%测试结果摘要：%RESET%
if %unit_result%==0 (echo %GREEN%  ✓ 单元测试: 通过%RESET%) else (echo %RED%  ✗ 单元测试: 失败%RESET%)
if %integration_result%==0 (echo %GREEN%  ✓ 集成测试: 通过%RESET%) else (echo %RED%  ✗ 集成测试: 失败%RESET%)
if %performance_result%==0 (echo %GREEN%  ✓ 性能测试: 通过%RESET%) else (echo %YELLOW%  ! 性能测试: 有警告%RESET%)
echo.

:: 计算总体结果
set /a "total_errors=%unit_result%+%integration_result%"
if %total_errors%==0 (
    echo %GREEN%🎉 所有关键测试通过！%RESET%
    exit /b 0
) else (
    echo %RED%❌ 有 %total_errors% 个关键测试失败%RESET%
    exit /b 1
)

:error_exit
echo.
echo %RED%========================================%RESET%
echo %RED%测试执行中断%RESET%
echo %RED%========================================%RESET%
echo %RED%请检查错误日志: %REPORT_DIR%\%RESET%
pause
exit /b 1
echo.

cd ..
pause